package com.imedway.ihe.dtz.xds.util;

import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Code;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntryType;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindFoldersQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindSubmissionSetsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetAllQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetAssociationsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetDocumentsAndAssociationsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFolderAndContentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFoldersForDocumentQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetSubmissionSetAndContentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetSubmissionSetsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.QueryList;

import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.FindFoldersVO;
import com.imedway.ihe.dd.vo.FindSubmissionSetsVO;
import com.imedway.ihe.dd.vo.GetAllVO;
import com.imedway.ihe.dd.vo.GetAssociationsVO;
import com.imedway.ihe.dd.vo.GetDocumentsAndAssociationsVO;
import com.imedway.ihe.dd.vo.GetFolderAndContentsVO;
import com.imedway.ihe.dd.vo.GetFoldersForDocumentVO;
import com.imedway.ihe.dd.vo.GetSubmissionSetsVO;
import com.imedway.ihe.dtz.xds.vo.GetDocumentsVO;
import com.imedway.ihe.dtz.xds.vo.GetSubmissionSetAndContentsVO;

public class BuildVoUtil {

	public static FindDocumentsVO buildFindDocumentsVO(FindDocumentsQuery findDocumentsQuery) {
		FindDocumentsVO findDocumentsVO = new FindDocumentsVO();
		List<String> authorPersonList = (List<String>) findDocumentsQuery.getAuthorPersons();
		if(authorPersonList!=null&&!authorPersonList.isEmpty()) {
			findDocumentsVO.set$XDSDocumentEntryAuthorPerson(authorPersonList.get(0));
		}
		List<Code> codeList= (List<Code>) findDocumentsQuery.getClassCodes();
		if(codeList!=null&&!codeList.isEmpty()) {
			findDocumentsVO.set$XDSDocumentEntryClassCode(codeList.get(0).getCode());
		}
		if(findDocumentsQuery.getCreationTime()!=null&&findDocumentsQuery.getCreationTime().getFrom()!=null) {
			findDocumentsVO.set$XDSDocumentEntryCreationTimeFrom(findDocumentsQuery.getCreationTime().getFrom().toString());
		}
		if(findDocumentsQuery.getCreationTime()!=null&&findDocumentsQuery.getCreationTime().getTo()!=null) {
			findDocumentsVO.set$XDSDocumentEntryCreationTimeTo(findDocumentsQuery.getCreationTime().getTo().toString());
		}
		List<Code> hcodeList = (List<Code>) findDocumentsQuery.getHealthcareFacilityTypeCodes();
		if(hcodeList!=null&&!hcodeList.isEmpty()) {
			findDocumentsVO.set$XDSDocumentEntryHealthcareFacilityTypeCode(hcodeList.get(0).getCode());
		}
		findDocumentsVO.set$XDSDocumentEntryPatientId(findDocumentsQuery.getPatientId().getId());
		if(findDocumentsQuery.getServiceStartTime()!=null&&findDocumentsQuery.getServiceStartTime().getFrom()!=null) {
			findDocumentsVO.set$XDSDocumentEntryServiceStartTimeFrom(findDocumentsQuery.getServiceStartTime().getFrom().toString());
		}
		if(findDocumentsQuery.getServiceStartTime()!=null&&findDocumentsQuery.getServiceStartTime().getTo()!=null) {
			findDocumentsVO.set$XDSDocumentEntryServiceStartTimeTo(findDocumentsQuery.getServiceStartTime().getTo().toString());
		}
		List<AvailabilityStatus> availabilityStatusList=(List<AvailabilityStatus>) findDocumentsQuery.getStatus();
		if(availabilityStatusList!=null&&!availabilityStatusList.isEmpty()) {
			findDocumentsVO.set$XDSDocumentEntryStatus(availabilityStatusList.get(0).getOpcode());

		}
		List<Code> tcodeList= (List<Code>) findDocumentsQuery.getTypeCodes();
		if(tcodeList!=null&&!tcodeList.isEmpty()) {
			findDocumentsVO.set$XDSDocumentEntryTypeCode(tcodeList.get(0).getCode());
		}
		
		return findDocumentsVO;
		
	}

	public static FindSubmissionSetsVO buildFindSubmissionSetsVO(FindSubmissionSetsQuery query) {
		FindSubmissionSetsVO vo = new FindSubmissionSetsVO();
		vo.set$XDSSubmissionSetAuthorPerson(query.getAuthorPerson());
		List<Code> codeList = (List<Code>)query.getContentTypeCodes();
		if(codeList!=null&&!codeList.isEmpty()) {
			vo.set$XDSSubmissionSetContentType(codeList.get(0).getCode());
		}
		vo.set$XDSSubmissionSetPatientId(query.getPatientId().getId());
		List<String> sourceIdList = (List<String>) query.getSourceIds();
		if(sourceIdList!=null&&!sourceIdList.isEmpty()) {
			vo.set$XDSSubmissionSetSourceId(sourceIdList.get(0));
		}
		List<AvailabilityStatus> availabilityStatusList=(List<AvailabilityStatus>) query.getStatus();
		if(availabilityStatusList!=null&&!availabilityStatusList.isEmpty()) {
			vo.set$XDSSubmissionSetStatus(availabilityStatusList.get(0).getOpcode());
		}
		if(query.getSubmissionTime()!=null&&query.getSubmissionTime().getFrom()!=null) {
	        vo.set$XDSSubmissionSetSubmissionTimeFrom(query.getSubmissionTime().getFrom().toString());
		}
		if(query.getSubmissionTime()!=null&&query.getSubmissionTime().getTo()!=null) {
	        vo.set$XDSSubmissionSetSubmissionTimeTo(query.getSubmissionTime().getTo().toString());
		}
		return vo;
	}

	public static FindFoldersVO buildFindFoldersVO(FindFoldersQuery query) {
		FindFoldersVO vo = new FindFoldersVO();	
		QueryList<Code> q= query.getCodes();
		if(q!=null) {
			vo.set$XDSFolderCodeList(q.getOuterList().get(0).get(0).getCode());
		}
		if(query.getLastUpdateTime()!=null&&query.getLastUpdateTime().getFrom()!=null) {
			vo.set$XDSFolderLastUpdateTimeFrom(query.getLastUpdateTime().getFrom().toString());
		}
		if(query.getLastUpdateTime()!=null&&query.getLastUpdateTime().getTo()!=null) {
			vo.set$XDSFolderLastUpdateTimeTo(query.getLastUpdateTime().getTo().toString());	
		}
		if(query.getPatientId()!=null) {
			vo.set$XDSFolderPatientId(query.getPatientId().getId());
		}
		List<AvailabilityStatus> availabilityStatusList=(List<AvailabilityStatus>) query.getStatus();
		if(availabilityStatusList!=null&&!availabilityStatusList.isEmpty()) {
			vo.set$XDSFolderStatus(availabilityStatusList.get(0).getOpcode());
		}
		return vo;
	}
	
	public static GetAssociationsVO buildGetAssociationsVO(GetAssociationsQuery query) {
		GetAssociationsVO vo = new GetAssociationsVO();
		vo.set$homeCommunityId(query.getHomeCommunityId());
		List<String> uuidList = (List<String>) query.getUuids();
		if(uuidList!=null&&!uuidList.isEmpty()) {
			vo.set$uuid(uuidList.get(0));
		}
		return vo;
	}
	
	public static GetDocumentsAndAssociationsVO buildGetDocumentsAndAssociationsVO(GetDocumentsAndAssociationsQuery query) {
		GetDocumentsAndAssociationsVO vo = new GetDocumentsAndAssociationsVO();
		vo.set$homeCommunityId(query.getHomeCommunityId());
		List<String> uuidList = (List<String>) query.getUuids();
		if(uuidList!=null&&!uuidList.isEmpty()) {
			vo.set$XDSDocumentEntryEntryUUID(uuidList.get(0));
		}
		List<String> uniqueIdList = (List<String>) query.getUniqueIds();
		if(uniqueIdList!=null&&!uniqueIdList.isEmpty()) {
			vo.set$XDSDocumentEntryUniqueId(uniqueIdList.get(0));
		}
		return vo;		
	}
	
	public static GetSubmissionSetsVO buildGetSubmissionSetsVO(GetSubmissionSetsQuery query) {
		GetSubmissionSetsVO vo = new GetSubmissionSetsVO();
		vo.set$homeCommunityId(query.getHomeCommunityId());
		List<String> uuidList = (List<String>) query.getUuids();
		if(uuidList!=null&&!uuidList.isEmpty()) {
			vo.set$uuid(uuidList.get(0));
		}
		return vo;
	}
	
	
	public static GetFolderAndContentsVO buildGetFolderAndContentsVO(GetFolderAndContentsQuery query) {
		GetFolderAndContentsVO vo = new GetFolderAndContentsVO();
		vo.set$homeCommunityId(query.getHomeCommunityId());
		QueryList<Code> q= query.getConfidentialityCodes();
		if(q!=null) {
			vo.set$XDSDocumentEntryConfidentialityCode(q.getOuterList().get(0).get(0).getCode());
		}
		List<Code> tcodeList= (List<Code>) query.getFormatCodes();
		if(tcodeList!=null&&!tcodeList.isEmpty()) {
			vo.set$XDSDocumentEntryFormatCode(tcodeList.get(0).getCode());
		}
		List<DocumentEntryType> list= (List<DocumentEntryType>) query.getDocumentEntryTypes();
		if(list!=null&&!list.isEmpty()) {
			vo.set$XDSDocumentEntryType(list.get(0).getUuid());
		}	 
		vo.set$XDSFolderEntryUUID(query.getUuid());
		vo.set$XDSFolderUniqueId(query.getUniqueId());
	
		return vo;
	}
	
	public static GetFoldersForDocumentVO buildGetFoldersForDocumentVO(GetFoldersForDocumentQuery query) {
		GetFoldersForDocumentVO vo = new GetFoldersForDocumentVO();
		vo.set$homeCommunityId(query.getHomeCommunityId());
		vo.set$XDSDocumentEntryEntryUUID(query.getUuid());
		vo.set$XDSDocumentEntryUniqueId(query.getUniqueId());		
		return vo;
	}
	
	public static GetAllVO buildGetAllVO(GetAllQuery query) {
		GetAllVO vo = new GetAllVO();
		vo.set$patientId(query.getPatientId().getId());
		QueryList<Code> q= query.getConfidentialityCodes();
		if(q!=null) {
			vo.set$XDSDocumentEntryConfidentialityCode(q.getOuterList().get(0).get(0).getCode());
		}
		List<Code> tcodeList= (List<Code>) query.getFormatCodes();
		if(tcodeList!=null&&!tcodeList.isEmpty()) {
			vo.set$XDSDocumentEntryFormatCode(tcodeList.get(0).getCode());
		}
		List<AvailabilityStatus> availabilityStatusList=(List<AvailabilityStatus>) query.getStatusDocuments();
		if(availabilityStatusList!=null&&!availabilityStatusList.isEmpty()) {
			vo.set$XDSDocumentEntryStatus(availabilityStatusList.get(0).getOpcode());
		}
		List<DocumentEntryType> list= (List<DocumentEntryType>) query.getDocumentEntryTypes();
		if(list!=null&&!list.isEmpty()) {
			vo.set$XDSDocumentEntryType(list.get(0).getUuid());
		}	
		List<AvailabilityStatus> favailabilityStatusList=(List<AvailabilityStatus>) query.getStatusFolders();
		if(favailabilityStatusList!=null&&!favailabilityStatusList.isEmpty()) {
			vo.set$XDSFolderStatus(favailabilityStatusList.get(0).getOpcode());
		}
		List<AvailabilityStatus> savailabilityStatusList=(List<AvailabilityStatus>) query.getStatusSubmissionSets();
		if(savailabilityStatusList!=null&&!savailabilityStatusList.isEmpty()) {
			vo.set$XDSSubmissionSetStatus(availabilityStatusList.get(0).getOpcode());
		}
		return vo;
	}
		
	public static GetSubmissionSetAndContentsVO buildGetSubmissionSetAndContentsVO(GetSubmissionSetAndContentsQuery query) {
		GetSubmissionSetAndContentsVO vo = new GetSubmissionSetAndContentsVO();
		vo.set$homeCommunityId(query.getHomeCommunityId());

		if(query.getConfidentialityCodes()!=null) {
			vo.set$XDSDocumentEntryConfidentialityCode(query.getConfidentialityCodes().getOuterList().get(0).get(0).getCode());
		}
		if(query.getFormatCodes()!=null) {
			vo.set$XDSDocumentEntryFormatCode(query.getFormatCodes().get(0).getCode());
		}
		if(query.getDocumentEntryTypes()!=null) {
			vo.set$XDSDocumentEntryType(query.getDocumentEntryTypes().get(0).getUuid());
		}
		vo.set$XDSSubmissionSetEntryUUID(query.getUuid());
		vo.set$XDSSubmissionSetUniqueId(query.getUniqueId());
		return vo;
	}
	
	public static GetDocumentsVO buildGetDocumentsVO(GetDocumentsQuery getDocumentsQuery) {
		GetDocumentsVO getDocumentsVO = new GetDocumentsVO();
		getDocumentsVO.set$XDSDocumentEntryEntryUUID(getDocumentsQuery.getUuids());
		getDocumentsVO.set$homeCommunityId(getDocumentsQuery.getHomeCommunityId());
		getDocumentsVO.set$XDSDocumentEntryUniqueId(getDocumentsQuery.getUniqueIds());				
		return getDocumentsVO;
		
	}
}
